package domain.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.StringTokenizer;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.jsr107cache.Cache;
import net.sf.jsr107cache.CacheException;
import net.sf.jsr107cache.CacheManager;

import domain.data.QnAPersistenceManager;
import domain.model.Whois;
import domain.service.Utils;

@SuppressWarnings("serial")
public class WhoisServlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        resp.getWriter().println("Hello, world");
        Date date_cache = new Date(new Date().getTime()+60*60*1000);
		
		resp.setHeader( "Cache-Control", "public, max-age=30");
		resp.setHeader( "Expires", date_cache.toGMTString() );
//        PersistenceManager psm = QnAPersistenceManager.get().getPersistenceManager();
//        
//        String path = ((HttpServletRequest)req).getRequestURI();
//		StringTokenizer st = new StringTokenizer( path,"/");
//        int count = st.countTokens(); 
//
//        if(count != 2)
//        {
//        	resp.sendRedirect("/");
//        }
//        
//		// skip one token /question/abcd (remove sites)
//		st.nextToken();
//		String title_url = st.nextToken();
//        title_url = title_url.toLowerCase().replaceAll("(http|https)+://(www.)*", "").replaceAll("^[\\w\\W]+[\\/]{2}", "").replaceAll("[\\/]+[\\w\\W]+$", "").replaceAll("www.", "");
//        
//        Query query = psm.newQuery(Whois.class);
//        query.setFilter("name=='"+title_url+"'");
//        @SuppressWarnings("unchecked")
//		List<Whois> listWhois = (List<Whois>) query.execute();
//        
//        if(listWhois.size() > 0)
//        {
//	        req.setAttribute("listWhois", listWhois.get(0));
//	        
//	        Cache cache=null;
//
//	        try {
//	            cache = CacheManager.getInstance().getCacheFactory().createCache(Collections.emptyMap());
//	        } catch (CacheException e) {
//	           e.printStackTrace();
//	           
//	        }
//	        if(cache.containsKey("lastPingWhois"))
//	        {
//	        	@SuppressWarnings("unchecked")
//				ArrayList<String> lastPing = (ArrayList<String>)cache.get("lastPingWhois");
//	        	if(lastPing.size()>19)
//	        	{
//	        		lastPing.remove(0);
//	        		
//	        		
//	        	}
//	        	if(!lastPing.contains(listWhois.get(0).getName()))
//	        	lastPing.add(listWhois.get(0).getName());
//	        	cache.put("lastPingWhois", lastPing);
//	        	
//	        }
//	        else
//	        {
//	        	
//	        	ArrayList<String> lastPing = new ArrayList<String>();
//	        	if(!lastPing.contains(listWhois.get(0).getName()))
//	        	lastPing.add(listWhois.get(0).getName());
//	        	cache.put("lastPingWhois", lastPing);
//	        }
//	        req.setAttribute("title", "Whois lookup : Domain "+title_url);
//			req.setAttribute("description", "Whois lookup : Domain "+title_url+".Use Our Free Whois Lookup Database To Search For & Reserve Your Domain Today at  "+Utils.ValidDomain());
//			req.setAttribute("keyword", title_url+",whois,whois lookup,Domain name information,  Server information, Web site analysis, website traffic details, Website backlinks website IP, website ranking info");
//	        
//	        try {
//				req.getRequestDispatcher("/whois.jsp").forward(req, resp);
//			} catch (ServletException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//        } else {
//        	new domain.service.Whois(title_url);
//        	resp.sendRedirect("/whois/"+title_url);
//        }
    }
}
